package com.jvup.gameshow.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jvup.common.utils.EquipmentInfoUtil;
import com.jvup.gameshow.R;

@SuppressLint("NewApi")
public class HaveIconLayout extends LinearLayout {

    ImageView mIcon;
    float iconWidth;
    float iconHeight;
    Drawable DrawableNormal;
    Drawable DrawablePress;
    int pressColor;

    public HaveIconLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initIcon(context, attrs);
        addIconView(context);
    }

    private void initIcon(Context context, AttributeSet attrs) {
        int dp20 = EquipmentInfoUtil.dip2px(20);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HaveIconLayout);
        iconWidth = a.getDimension(R.styleable.HaveIconLayout_iconWidth, dp20);
        iconHeight = a.getDimension(R.styleable.HaveIconLayout_iconHeight, dp20);
        DrawableNormal = a.getDrawable(R.styleable.HaveIconLayout_iconNormal);
        DrawablePress = a.getDrawable(R.styleable.HaveIconLayout_iconPress);
        pressColor = a.getColor(R.styleable.HaveIconLayout_pressBackground, 0x00000000);
    }

    private void addIconView(Context context) {
        if (mIcon == null) {
            mIcon = new ImageView(context);
            mIcon.setBackground(DrawableNormal);
            setIconWidthAndHeight((int) iconWidth, (int) iconHeight);
            addView(mIcon);
        }
    }
    private void setIconWidthAndHeight(int iconWidth, int iconHeight) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(iconWidth, iconHeight);
        mIcon.setLayoutParams(params);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mIcon.setBackground(DrawablePress);
                setBackgroundColor(pressColor);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mIcon.setBackground(DrawableNormal);
                setBackgroundColor(0x00000000);
                break;
        }
        return super.onTouchEvent(event);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }




}
